Imports DaveSexton.DocProject.DocSites

Partial Public Class DocSiteContents
	Inherits System.Web.UI.UserControl

	<System.ComponentModel.Browsable(False)> _
	Public ReadOnly Property SelectedTopicFullTitle() As String
		Get
			Return String.Format(System.Globalization.CultureInfo.CurrentCulture, _
			 CStr(GetGlobalResourceObject("General", "TopicTitleFormat")), DocSite.ProjectTitle, SelectedTopicTitle)
		End Get
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public ReadOnly Property SelectedTopicTitle() As String
		Get
			Dim topic As String = SelectedTopic
			Return topic.Substring(topic.LastIndexOf("/") + 1)
		End Get
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public ReadOnly Property SelectedTopicUrl() As String
		Get
			Return New Uri(Request.Url, DocSiteNavigator.GetTopicUrl(SelectedTopic, True)).ToString()
		End Get
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Property SelectedTopic() As String
		Get
			Dim node As TreeNode = contentsTreeView.SelectedNode

			If node Is Nothing Then
				Return String.Empty
			Else
				Return node.ValuePath
			End If
		End Get
		Set(ByVal value As String)
			If contentsTreeView.Nodes.Count = 0 Then
				contentsTreeView.DataBind()
			End If

			Dim node As TreeNode = FindTopicNode(value)

			If Not node Is Nothing Then
				If Not node.Selected Then
					node.Select()
				End If

				Do
					If Not node.Expanded.GetValueOrDefault() Then
						node.Expand()
					End If

					node = node.Parent
				Loop While Not node Is Nothing
			End If
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public ReadOnly Property DefaultTopic() As String
		Get
			If contentsTreeView.Nodes.Count > 0 Then
				Return contentsTreeView.Nodes(0).Value
			Else
				Return String.Empty
			End If
		End Get
	End Property

	Private Function FindTopicNode(ByVal topic As String) As TreeNode
		If contentsTreeView.Nodes.Count = 0 Then
			Return Nothing
		ElseIf String.IsNullOrEmpty(topic) Then
			Return contentsTreeView.Nodes(0)
		Else
			Dim topics As List(Of String) = New List(Of String)(topic.Split(New Char() {CChar("/")}, StringSplitOptions.RemoveEmptyEntries))

			If topics.Count = 0 Then
				Return Nothing
			Else
				' this method is used in place of TreeView.FindNode because it supports delayed loading
				Return FindTopicNodeRecursive(topics, contentsTreeView.Nodes)
			End If
		End If
	End Function

	Private Function FindTopicNodeRecursive(ByVal topics As List(Of String), ByVal nodes As TreeNodeCollection) As TreeNode
		For Each currentNode As TreeNode In nodes
			If topics(0).Equals(currentNode.Text, StringComparison.Ordinal) Then
				topics.RemoveAt(0)

				If topics.Count = 0 Then
					Return currentNode
				ElseIf Not currentNode.Expanded.GetValueOrDefault(False) Then
					' This call is required for nodes where PopulateOnDemand is true; otherwise, ChildNodes is always empty.
					currentNode.Expand()
				End If

				Dim foundNode As TreeNode = FindTopicNodeRecursive(topics, currentNode.ChildNodes)

				If Not foundNode Is Nothing Then
					Return foundNode
				End If
			End If
		Next

		Return Nothing
	End Function

	Public Function GetSyncTocClientCallback() As String
		Return Page.ClientScript.GetPostBackEventReference(tocSyncButton, String.Empty)
	End Function

	Public Event SelectedTopicChanged(ByVal sender As Object, ByVal e As EventArgs)

	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
		MyBase.OnInit(e)

		Dim data As String = DocSiteNavigator.DocSiteContentsDocument.CreateNavigator().OuterXml
		contentsXmlDataSource.Data = data

		ScriptManager.RegisterStartupScript(Page, Me.GetType(), "initializeTocButtons", "initializeTocButtons();", True)
	End Sub

	Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
		Dim subject As String = Server.HtmlEncode(String.Format(System.Globalization.CultureInfo.CurrentCulture, _
		 CStr(GetGlobalResourceObject("General", "EmailTopicUrlSubjectFormat")), DocSite.ProjectTitle, SelectedTopicTitle, SelectedTopicUrl))

		Dim body As String = Server.HtmlEncode(String.Format(System.Globalization.CultureInfo.CurrentCulture, _
		 CStr(GetGlobalResourceObject("General", "EmailTopicUrlBodyFormat")), DocSite.ProjectTitle, SelectedTopic, SelectedTopicUrl))

		emailImageButton.OnClientClick = "emailUrl(event, '" & subject & "', '" & body & "'); return false;"

		MyBase.OnPreRender(e)
	End Sub

	Protected Sub contentsTreeView_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs)
		RaiseEvent SelectedTopicChanged(Me, e)
	End Sub
End Class